#include "File.h"
#include <cstdio>



File::File() 
{
}

File::~File() 
{
}

void File::read  ( const char* filename )
{
    // open binary
    FILE *fp = fopen(filename, "rb");
    if(fp)
    {
        // determine length
        fseek(fp, 0, SEEK_END);
        int length = ftell(fp);
        rewind(fp);

        // resize and read
        m_bytes.resize(length);
        fread(m_bytes[0], sizeof(char), length, fp);
        fclose(fp);
    }    
}

void File::write ( const char* filename ) const
{
    FILE *fp = fopen(filename, "wb");
    if(fp)
    {
        unsigned int len = m_bytes.length();
        fwrite(m_bytes[0], sizeof(char), len, fp);
        fclose(fp);
    }
}

unsigned int File::length() const
{
    return m_bytes.length();
}

char* File::operator[](int idx)
{
    return m_bytes[idx];
}

ByteBuffer& File::get_buffer()
{
    return m_bytes;
}

const ByteBuffer& File::get_buffer() const
{
    return m_bytes;
}